<!-- 项目附件信息 -->
<template>
  <TCollapse v-if="+showType" title="项目附件信息">
    <iframe :src="attachmentInfoUrl" scrolling="no" width="100%" :height="attachmentHeight" frameborder="0" />
  </TCollapse>
  <iframe v-else :src="attachmentInfoUrl" scrolling="no" width="100%" :height="attachmentHeight" frameborder="0" />
</template>
<script>

import TCollapse from '@/components/basic/TCollapse.vue'
import { getQueryObject } from '@/utils'
import { getWFWorkItemApi } from '@/api/acceptanceManagement/acceptanceMange'
import { getPrjDetail } from '@/api/actualBudget'

export default {
  name: 'JnSiteDetail',
  components: { TCollapse },
  props: {
    // 项目阶段
    prjStage: {
      type: String,
      default: '',
      required: true
    },
      showType: {
        type: String,
        default: '1'
    },
    prjType: {
        type: String
    },
    // 针对于特别修正=>结算送审附件
    spaclTag:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      attachmentInfoUrl: '',
      attachmentHeight: 190
    }
  },
  methods:{
   async sendIframe(){
      window.addEventListener('message', (event) => {
      const base_url = ''
      if (event.origin === base_url) {
        return
      }
      if (event.data.type === 'attachment') {
        console.log('附件传递高度', event.data)
        this.attachmentHeight = event.data.height
      }
    })
    },
    // 2024-9-26 阻断问题if过多解决方案
   async optimizeNewOne(bizObject,type, url, prjId, activityDefID, wfStage, buildModes, prjType,businessMajor,energyBusinessType,orgId,projectId){
      // 针对特别修正结算送审的处理
      await this.optimizeNewTwo(bizObject,type, url, prjId, activityDefID, wfStage, buildModes, prjType,businessMajor,energyBusinessType,orgId,projectId)

      if (type === 'done') { // 如果是已办,则只能查看
        this.attachmentInfoUrl = `${process.env.VUE_APP_ZHONGRUAN_URL}${url}?prjId=${prjId}&btnConfig=B&nodeId=${activityDefID}&prjStage=${wfStage}&prjType=${prjType}&buildModes=${buildModes}&towerType=0&roomType=0&businessMajor=${businessMajor}&businessType=${energyBusinessType}&orgId=${orgId}&projectId=${projectId}`
        } else {
        // 如果不是会签走正常的逻辑
        if (bizObject.bizState !== 'I2') {
          this.attachmentInfoUrl = `${process.env.VUE_APP_ZHONGRUAN_URL}${url}?prjId=${prjId}&nodeId=${activityDefID}&prjStage=${wfStage}&prjType=${prjType}&buildModes=${buildModes}&towerType=0&roomType=0&businessMajor=${businessMajor}&businessType=${energyBusinessType}&orgId=${orgId}&projectId=${projectId}`
        } else {
          this.attachmentInfoUrl = `${process.env.VUE_APP_ZHONGRUAN_URL}${url}?prjId=${prjId}&nodeId=${activityDefID}&prjStage=${wfStage}&prjType=${prjType}&buildModes=${buildModes}&towerType=0&roomType=0&btnConfig=B&businessMajor=${businessMajor}&businessType=${energyBusinessType}&orgId=${orgId}&projectId=${projectId}`
        }
      }

    },
    async optimizeNewTwo(bizObject,type, url, prjId, activityDefID, wfStage, buildModes, prjType,businessMajor,energyBusinessType,orgId,projectId) {
      if (this.spaclTag) {
        this.attachmentInfoUrl = `${process.env.VUE_APP_ZHONGRUAN_URL}${url}?prjId=${prjId}&nodeId=${activityDefID}&prjStage=${wfStage}&prjType=${prjType}&buildModes=${buildModes}&towerType=0&roomType=0&businessMajor=${businessMajor}&btnConfig=B&businessType=${energyBusinessType}&orgId=${orgId}&projectId=${projectId}`
        throw ''
      }
    },
    // 综合查询进来的特别修正结算送审
    async optimizeNewThree(prjId){
      // 针对特别修正 审减退库综合查询需要传递变更流水号查询附件信息
      const {bizseq,adjustId} = this.$route.query
      const projectIdNew = ["AM","SJ"].includes(this.prjStage)?(bizseq||adjustId):prjId
      const { data: { buildModes, prjType, towerType, roomType ,orgId} } = await getPrjDetail({ prjId })
      let urls = ''
      if(this.spaclTag){
        urls = `${process.env.VUE_APP_ZHONGRUAN_URL}attachmentInfoTodo?prjType=${this.prjType}&buildModes=${buildModes}&towerType=${towerType}&roomType=${roomType}&prjId=${prjId}&prjStage=${this.prjStage}&isChange=0&operateType=1&btnConfig=B`
      }else{
        urls = `${process.env.VUE_APP_ZHONGRUAN_URL}attachmentInfo?prjId=${projectIdNew}&prjStage=${this.prjStage}&btnConfig=B`
      }
      return urls
    },
  },
  async created() {
    const { workItemId = '', type = '' } = getQueryObject()
    let { prjId = '' } = getQueryObject()
    if (!type) {
      const { data: { buildModes, prjType, towerType, roomType ,orgId} } = await getPrjDetail({ prjId })
      this.attachmentInfoUrl = `${process.env.VUE_APP_ZHONGRUAN_URL}attachmentInfo?prjId=${prjId}&nodeId=SERVICE_CONFIRM_NODE&prjStage=Q&prjType=${prjType}&buildModes=${buildModes}&towerType=${towerType}&roomType=${roomType}&orgId=${orgId}&projectId=${prjId}`
    } else if (+type === 1) { // 综合查询进入,从路由获取prjId
      this.attachmentInfoUrl = await this.optimizeNewThree(prjId)
    } else { // 从工作台进入,从路由获取workItemId,调用接口获取prjId
      const { data } = await getWFWorkItemApi(workItemId)
      const { bizObject, activityDefID } = data
      const { wfStage } = bizObject
      prjId = bizObject?.bizSeq
      let projectId = bizObject?.prjId
      const { data: basicData } = await getPrjDetail({ prjId:projectId })
      const { prjType, buildModes,businessMajor,energyBusinessType,orgId,isNewbusiness} = basicData
      const show = isNewbusiness==='1'&&['25','29'].includes(buildModes)&&wfStage==='IN'
      if(show){
        prjId = bizObject?.bizSeq
      }
      let url = 'attachmentInfo'
      if (this.prjType) {
        url = 'attachmentInfoTodo'
      }
      await this.optimizeNewOne(bizObject,type, url, prjId, activityDefID, wfStage, buildModes, prjType,businessMajor,energyBusinessType,orgId,projectId)
    }
    await this.sendIframe()
  }
}
</script>

<style scoped lang='scss'>
</style>
